﻿package com.skillscoach  {
	import fl.video.FLVPlayback;
	import fl.video.MetadataEvent;
	import fl.video.VideoEvent;
	import fl.video.VideoProgressEvent;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	
	
	public class Seekbar extends MovieClip{
		
		private var timerDisplay:Timer;	
		private var playerTimer:Timer;	
		public var videoClip:MovieClip;
		private const TIMER_DELAY:int	= 10;
		public var videoPlayPauseBtn:MovieClip;
		
		
		public static const HOURS:uint = 2;
		public static const MINUTES:uint = 1;
		public static const SECONDS:uint = 0;
		public var dataObj:DataObject;
		
		public function Seekbar()  
		{
			// constructor code
			down_mask.scaleX = 0;
			seekbar_mask.scaleX = 0;
		}
		
		public function initControls(vClip:MovieClip, videoPlayPause:MovieClip,pTimer:Timer):void
		{
			videoClip = vClip;
			videoPlayPauseBtn = videoPlayPause;		
			playerTimer = pTimer;
			positionHandle.addEventListener(MouseEvent.MOUSE_DOWN,mDown);			
			timerDisplay = new Timer(TIMER_DELAY);			
			timerDisplay.addEventListener(TimerEvent.TIMER, updateDisplay);
			timerDisplay.start();
		}
		public function updateVideoStatus(e:Event):void
		{
			trace(e.target.objInfo.duration);
		}
		 

		public function progressHandler(event:VideoProgressEvent):void
		{
			var bl:Number = Math.round(event.bytesLoaded/1000);
			var bt:Number = Math.round(event.bytesTotal/1000);
			down_mask.scaleX = bl/bt;
		}
		public function updateDisplay(event:TimerEvent):void
		{
			
			var perTime:Number = videoClip.currentFrame/videoClip.totalFrames;
			if(perTime < 1)
			{
				seekbar_mask.scaleX = perTime ;
				this.timerText_txt.text = formatTime(videoClip.currentFrame/24)+ "/" + formatTime(videoClip.totalFrames/24);

			}else
			{
				seekbar_mask.scaleX = 1;
			}
			positionHandle.x = seekbar_mask.width-9;
		}
		private function formatTime(time:Number, detailLevel:uint = 2):String
		{
			
			var minutes:uint = time/60;
			var seconds:uint = (time%60);			
			var minuteString:String = detailLevel >= MINUTES ? ((detailLevel == HOURS && minutes <10 ? "0":"") + minutes + ":"):"";
			var secondString:String = ((seconds < 10 && (detailLevel >= MINUTES)) ? "0":"") + seconds;
			return minuteString + secondString;
			
		}
		private function mDown(event:MouseEvent):void
		{
			playerTimer.stop();
			timerDisplay.stop();
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mMove);
			stage.addEventListener(MouseEvent.MOUSE_UP,mUp);
		}
		private function mMove(event:MouseEvent):void
		{
			dataObj.HideImageByCuePoint(videoClip.currentFrame);
			if(0 <((stage.mouseX-243.5)/447.55) )
			{
				seekbar_mask.scaleX = (stage.mouseX-243.5)/447.55;
				positionHandle.x = seekbar_mask.width-9;
			}
			if(((stage.mouseX-243.5)/447.55)  > 1)
			{
				seekbar_mask.scaleX = 1;
				positionHandle.x =  seekbar_mask.width-9;
			}
			
			videoClip.gotoAndStop(int(seekbar_mask.scaleX*videoClip.totalFrames));
			
		}
		private function mUp(event:MouseEvent):void
		{
			
			if(0 <((stage.mouseX-243.5)/447.55) )
			{
				seekbar_mask.scaleX = (stage.mouseX-243.5)/447.55;
				positionHandle.x = seekbar_mask.width-9;
			}
			if(((stage.mouseX-243.5)/447.55)  > 1)
			{
				seekbar_mask.scaleX = 1;
				positionHandle.x =  seekbar_mask.width-9;
			}

			videoClip.gotoAndStop(int(seekbar_mask.scaleX*videoClip.totalFrames));
			
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,mMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP,mUp);
			timerDisplay.start();
			if(videoPlayPauseBtn.currentFrame == 2)
				playerTimer.start();
			
		}
	}
	
}
